﻿namespace Tomato.Allure.Samples
{
	public class OpenAll : AddOn
	{
		private OpenAllForm m_openSolutionFileForm = null;
		private bool m_bSolutionOpened = false;

		public override bool OnStart( IVisualStudio visualStudio )
		{
			// Form
			m_openSolutionFileForm = new OpenAllForm( visualStudio );

			return true;
		}

		public override bool OnEvent( IService allureService, AllureEvent allureEvent )
		{
			switch( allureEvent.EventType )
			{
				case EventType.SolutionOpened:
					{
						m_openSolutionFileForm.RebuildSolutionFileList();

						m_bSolutionOpened = true;
					}
					return true;

				case EventType.SolutionClosing:
					{
						m_bSolutionOpened = false;
					}
					return true;

				case EventType.ProjectItemAdded:
				case EventType.ProjectItemRemoved:
				case EventType.ProjectItemRenamed:
				case EventType.ProjectAdded:
				case EventType.ProjectRemoved:
				case EventType.ProjectRenamed:
				case EventType.SolutionItemAdded:
				case EventType.SolutionItemRemoved:
				case EventType.SolutionItemRenamed:
				case EventType.MiscFileItemAdded:
				case EventType.MiscFileItemRemoved:
				case EventType.MiscFileItemRenamed:
					{
						RebuildSolutionFileList();
					}
					return true;
			}

			return false;
		}

		public override bool OnExecute( IService allureService )
		{
			ShowOpenAllForm();
			return true;
		}

		protected void RebuildSolutionFileList()
		{
			if( m_bSolutionOpened 
				&& ( m_openSolutionFileForm != null ) )
			{
				m_openSolutionFileForm.RebuildSolutionFileList();
			}
		}

		protected void ShowOpenAllForm()
		{
			if( m_bSolutionOpened
							&& ( m_openSolutionFileForm != null ) )
			{
				m_openSolutionFileForm.ShowDialog();
			}
		}
	}
}
